<?php

class Training_Model_UserGroup extends Zend_Db_Table {

    protected $_name = 'user_group';
    protected $_primary = 'id';
    //protected $_primary = array('id1','id2');
    
    public function getItem5() {
        $db = Zend_Registry::get('connectDb');
//        $db = Zend_Db::factory($adapter, $config);
        
        $select = $db->select()
                     ->from('user_group')
                     ->where('group_acp = ?', 1, INTEGER);
        $result = $db->fetchAll($select);
        return $result;
    }
    
    public function getItem4() {
        echo $select = $this->select(Zend_Db_Table::SELECT_WITHOUT_FROM_PART)
                            ->setIntegrityCheck(false) // Khi o zend db table muon ket noi thi phai set ve false
                            ->from('user_group AS g', array('id', 'group_name'))
                            ->join('users AS u', 'u.group_id = g.id', array('user_name'));
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getItem3() {
        echo $select = $this->select()
                            ->from('user_group', array('id', 'group_name'))
                            ->where('group_acp = ?', 1, INTEGER)
                            ->order('group_name DESC');
        $result = $this->fetchAll($select)->toArray();
        return $result;
    }
    
    public function getItemPosition($position = null) {
        $where = 'group_acp = 1';
        $order = ' group_name ASC';
        //$result = $this->fetchAll($where,$order,$count,$offset);
        $result = $this->fetchAll($where, $order);
        $rowPostion = $result->seek($position);
        $result = $rowPostion->current()->toArray();
        return $result;
    }

    public function getItem2() {
        $where = ' group_acp = 1';
        $order = ' group_name ASC';
        $result = $this->fetchRow($where, $order)->toArray();
        return $result;
    }

    public function listItem() {
        $where = 'group_acp = 1 AND id <2';
        //$result = $this->fetchAll($where,$order,$count,$offset);
        $result = $this->fetchAll($where)->toArray();
        return $result;
    }

    public function listItem2() {
        $where = 'group_acp = 1';
        $order = ' group_name ASC';
        //$result = $this->fetchAll($where,$order,$count,$offset);
        $result = $this->fetchAll($where, $order)->toArray();
        return $result;
    }

    public function listItem3() {
        $count = 2;
        $offset = 2;
        //$result = $this->fetchAll($where,$order,$count,$offset);
        $result = $this->fetchAll(null, null, $count, $offset)->toArray();
        return $result;
    }

    public function getItem($id) {
        $result = $this->find($id)->toArray();
        return $result;
    }

    public function deleteItem($id) {
        $where = 'id = ' . $id;
        $this->delete($where);
    }

    public function addItem() {
        $data = array(
            'group_name' => 'Guest',
            'avatar' => 'Guest.png',
            'ranking' => 'rGuest.png',
            'group_acp' => 0,
            'group_default' => 1,
            'status' => 1,
            'order' => 10,
        );
        $row = $this->createRow($data);
        $row->save();
    }

    public function addItem2() {
        $row = $this->fetchNew();
        $row->group_name    = "Student";
        $row->avatar        = "student.png";
        $row->ranking       = "rStudent.png";
        $row->group_acp     = 0;
        $row->group_default = 0;
        $row->status        = 0;
        $row->order         = 12;
        $row->save();
    }

    public function addItem3() {
        $data = array(
            'group_name' => 'Vip',
            'avatar' => 'Vip.png',
            'ranking' => 'rVip.png',
            'group_acp' => 0,
            'group_default' => 0,
            'status' => 1,
            'order' => 10,
        );
        $this->insert($data);
    }

    public function updateItem() {
        $data = array(
            'group_name' => ' Vip 123',
            'avatar' => ' Vip_123.png',
        );
        $where = ' id  = 18';
        $this->update($data, $where);
    }

    public function updateItem2() {
        $where = " id = 18";
        $row = $this->fetchRow($where, $order);
        $row->group_name    = "Vip123456";
        $row->avatar        = "Vip123456.png";
        $row->save();
        echo '<pre>';
        print_r($row);
        echo '<pre>';
    }

}
